package top.zhangjianyong.tools.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.annotation.DoTimer;
import top.zhangjianyong.tools.utils.AnnotationUtil;
import top.zhangjianyong.tools.utils.ApiResponse;
import top.zhangjianyong.tools.utils.HolidayUtil;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.time.LocalDate;

/**
 * @author ： zhangjianyong
 * @date ： 2023/8/6 11:20
 */
@Slf4j
@Aspect
@Component
@Order(998)
public class StockAspect {


    @Value("${timer-flag}")
    private Boolean timerFlag;
    @Autowired
    private HolidayUtil holidayUtil;

    @Pointcut("@annotation(top.zhangjianyong.tools.annotation.DoTimer)")
    public void pointCut() {

    }
    @Around("pointCut()")
    public Object doTimer(ProceedingJoinPoint pjp) {

        log.info("进入日期检测");
        DoTimer doTimer = AnnotationUtil.getAnnotation(pjp, DoTimer.class);
        if (doTimer.open()) {
            if (!timerFlag) {
                log.info("定时任务关闭。。。");
                return null;
            }
        }
        if (1 == doTimer.dateType()) {
            LocalDate now = LocalDate.now();
            if (!holidayUtil.isStock(now)) {
                log.info("今天休市。。。");
                return null;
            }
        }

        try {
            log.info("当前日期符合执行");
            return pjp.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
            return ApiResponse.fail(e.getMessage());
        }
    }

}
